﻿using System;
using System.Windows.Forms;
using Yglu.BusinessLayer.Entities;

namespace Yglu.TerminalApp
{
	public partial class MainForm : Form
	{
		#region Properties

		private Order CurrentOrder { get; set; }

		#endregion

		#region Main methods

		public MainForm()
		{
			InitializeComponent();
		}

		private void MainForm_Load(object sender, EventArgs e)
		{
			if (Program.LoggedUser == null)
			{
				PopChangeUser();
			}

			if (Program.LoggedUser == null)
			{
				try
				{
					Application.Exit();
					Close();
				}
				catch
				{
				}
			}
			else
			{
				LoadAll();
			}
		}

		#endregion

		#region Loading

		private void LoadAll()
		{
			ResetFields();
			LoadUserDetails();
			LoadStoreDetails();
		}

		private void LoadStoreDetails()
		{
			statusItemLastOrder.Text = Program.LoggedTerminal.Store.Title;

			ctrProducts.LoadProducts();
		}

		private void LoadUserDetails()
		{
			lblLoggedUser.Text = Program.LoggedUser.FullName;
		}

		private void ResetFields()
		{
			CurrentOrder = new Order();
			CurrentOrder.User = Program.LoggedUser;
			CurrentOrder.Terminal = Program.LoggedTerminal;

			txtCustomer.Text = String.Empty;
			txtDiscount.Text = "0.00";
			txtTotalPrice.Text = "0.00";

			listCombos.Items.Clear();
			listCombos.SelectedIndex = -1;

			ctrProducts.Reset();
		}

		#endregion

		#region Change user

		private void butChangeUser_Click(object sender, EventArgs e)
		{
			PopChangeUser();
		}

		private void PopChangeUser()
		{
			Hide();

			using (ChangeUserForm form = new ChangeUserForm())
			{
				form.ShowDialog(this);
			}

			if (Program.LoggedUser != null)
			{
				LoadUserDetails();
				ResetFields();
				Show();
			}
		}

		#endregion

		#region Place order

		private void butAddCombo_Click(object sender, EventArgs e)
		{
			AddCombo();
		}

		private void AddCombo()
		{
			Combo c = ctrProducts.CurrentCombo;

			if (c.Products.Count < 1)
			{
				return;
			}

			if (c.PromotionApplied != null && c.PromotionApplied.Discount > 0)
			{
				txtDiscount.Text = (Convert.ToDouble(txtDiscount.Text) - c.PromotionApplied.Discount).ToString("0.00");
			}

			CurrentOrder.Combos.Add(ctrProducts.CurrentCombo);
			listCombos.Items.Add(ctrProducts.CurrentCombo);

			Promotion combosPromotion = null;

			foreach (Promotion p in Program.Promotions)
			{
				if (!p.Automatic)
				{
					continue;
				}

				if (p.Combos > 0 && CurrentOrder.Combos.Count >= p.Combos)
				{
					combosPromotion = p;
				}
			}

			if (combosPromotion != null)
			{
				CurrentOrder.PromotionApplied = combosPromotion;

				txtDiscount.Text = (Convert.ToDouble(txtDiscount.Text) - combosPromotion.Discount).ToString("0.00");
			}

			Double price = Convert.ToDouble(txtTotalPrice.Text);
			txtTotalPrice.Text = (price + c.TotalPrice).ToString("0.00");

			ctrProducts.Reset();

			if (CurrentOrder.DateOrdered < DateTime.Now.AddDays(-1))
			{
				CurrentOrder.DateOrdered = DateTime.Now;
			}
		}

		private void imgRemoveCombo_Click(object sender, EventArgs e)
		{
			if (listCombos.SelectedIndex < 0)
			{
				return;
			}

			Combo c = (Combo)listCombos.SelectedItem;

			CurrentOrder.Combos.RemoveAt(listCombos.SelectedIndex);
			listCombos.Items.RemoveAt(listCombos.SelectedIndex);

			Double price = Convert.ToDouble(txtTotalPrice.Text);
			txtTotalPrice.Text = (price - c.TotalPrice).ToString("0.00");

			if (c.PromotionApplied != null && c.PromotionApplied.Discount > 0)
			{
				txtDiscount.Text = (Convert.ToDouble(txtDiscount.Text) + c.PromotionApplied.Discount).ToString("0.00");
			}

			if (CurrentOrder.PromotionApplied != null && CurrentOrder.Combos.Count < CurrentOrder.PromotionApplied.Combos)
			{
				txtDiscount.Text = (Convert.ToDouble(txtDiscount.Text) + CurrentOrder.PromotionApplied.Discount).ToString("0.00");

				CurrentOrder.PromotionApplied = null;
			}

			if (CurrentOrder.Combos.Count < 1)
			{
				CurrentOrder.DateOrdered = DateTime.MinValue;
			}
		}

		#endregion

		#region Confirm order

		private void butConfirm_Click(object sender, EventArgs e)
		{
			if (listCombos.Items.Count < 1)
			{
				if (ctrProducts.CurrentCombo.Products.Count > 0)
				{
					AddCombo();	
				}
				else
				{
					Program.PopError(Messages.ValidationOrderIsEmpty);
					return;
				}
			}

			if (String.IsNullOrEmpty(txtCustomer.Text))
			{
				Program.PopError(Messages.ValidationCustomerBlank);
				txtCustomer.Focus();
				return;
			}

			CurrentOrder.Customer = txtCustomer.Text.Trim();
			CurrentOrder.Discount = Convert.ToDouble(txtDiscount.Text) * -1;
			CurrentOrder.TotalPrice = CurrentOrder.Combos.TotalPrice;

			using (PaymentForm pay = new PaymentForm(CurrentOrder))
			{
				if (pay.ShowDialog() == DialogResult.OK)
				{
					Program.PopConfirmation(Messages.OrderPlacedConfirmation.Replace("#", pay.CurrentOrder.Id.ToString()));
					statusItemLastOrder.Text = Messages.LastOrder + ": " + pay.CurrentOrder.Id;

					ResetFields();
				}
			}
		}

		private void butCancel_Click(object sender, EventArgs e)
		{
			if (Program.ConfirmAction(Messages.ConfirmCancelOrder))
			{
				ResetFields();
			}
		}

		#endregion

		#region Timers

		private void timerClock_Tick(object sender, EventArgs e)
		{
			statusItemClock.Text = DateTime.Now.ToString("HH:mm:ss");
		}

		#endregion
	}
}